package top.idcard.agent.biz.rw.read.local;

import com.google.common.primitives.Bytes;
import top.idcard.agent.biz.entity.Certification;
import top.idcard.agent.biz.rw.channel.CertificationByteDataTool;
import top.idcard.agent.biz.rw.read.AbstractRead;

import java.io.IOException;
import java.io.InputStream;
import java.nio.ByteBuffer;
import java.util.Objects;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * 从输入流读取数据，输入流是文件。即读取文件里写入的每一个证件信息
 *
 * @author wx
 * @version 1.0
 * @date 2021/2/1 11:13 上午
 */
public abstract class LocalCertificationRead extends AbstractRead<InputStream, Certification> {

    public LocalCertificationRead(InputStream input) {
        super(input);
    }

    public Certification nextObject() {
        Certification certification = null;
        try {
            byte[] length = new byte[4];
            input.read(length, 0, 1);
            Integer type = ByteBuffer.allocate(length.length).getInt(0);
            Integer len = Objects.requireNonNull(Certification.CertificationEnum.valueOf(type)).getByteLength();
            byte[] buffer = new byte[len - 1];
            input.read(buffer, 0, len - 1);
            certification = CertificationByteDataTool.getFromBytes(Bytes.concat(new byte[]{length[0]}, buffer));
        } catch (IOException e) {
            e.printStackTrace();
        }
        return certification;
    }
}
